﻿using FineUI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZHDJ.BranchCenter.BLL;
using ZHDJ.Data.Models.MassLine;

namespace ZHDJ.BranchCenter.Module.MassLine
{
    public partial class SuggestEdit : PageBase
    {
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "";
            }
        }
        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            string id = GetQueryIntValue("id");
            if (id != "")
            {
                PartyMassLineSuggest model = DB.PartyMassLineSuggests.Find(id);
                if (model == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                this.txtTitle.Text = model.Title;
                this.txtUserName.Text = model.UserName;
                this.txtContactWay.Text = model.ContactWay;
                this.txtCreateTime.Text = model.CreateTime?.ToString("yyyy-MM-dd");
                this.txtAddress.Text = model.Address;
                this.txtContent.Text = model.Content;
                this.txtReply.Text = model.Reply;
                if (model.ShowInHomePage == true)
                {
                    btnSaveClose.Visible = btnPublish.Visible = false;
                    btnUndo.Hidden = false;
                }
            }
        }
        #endregion

        #region Events

        private void SaveData(bool isPublish)
        {
            string id = GetQueryIntValue("id");
            string imageUrl = string.Empty;

            if (id != "")
            {
                //更新
                PartyMassLineSuggest model = DB.PartyMassLineSuggests.Find(id);
                if (model == null)
                {
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }
                if (isPublish)
                { model.ShowInHomePage = true; }
                else model.ShowInHomePage = false;
                if (!string.IsNullOrEmpty(this.txtReply.Text))
                    model.Statue = true;
                model.Reply = this.txtReply.Text;
                model.ModifyTime = DateTime.Now;
                model.ModifyBy = CurrentUser.LoginID;

                DB.SaveChanges();
            }
        }

        /// <summary>
        /// 保存并关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveData(false);

            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

        protected void btnPublish_Click(object sender, EventArgs e)
        {
            SaveData(true);

            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion

        protected void btnUndo_Click(object sender, EventArgs e)
        {
            SaveData(false);

            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
    }
}